package edu.hhu.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.omg.CORBA.Request;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sun.org.apache.xpath.internal.SourceTree;

import edu.hhu.model.CheckBox;
import edu.hhu.model.User;
import tree.TreeService;

public class TreeController extends Controller {

	public void tree() {
		HttpSession session = this.getSession();
		String json = (String) session.getAttribute("treeJson");

		// System.out.println(new Date());
		if (json == null) {
			TreeService xzqService = new TreeService();
			try {
				json = JSON.toJSONString(xzqService.tree());
				session.setAttribute("treeJson", json);
			} catch (Exception e) {

			}
		}
		// System.out.println(new Date());
		// System.out.println(json);
		renderJson(json);
	}

	public void checkbox() {
		String level = this.getPara("level");
		String json = "";
		List<Record> data = Db.use("mysql").find("select * from dictionary t where t.level <= " + level + " ORDER BY t.`level` ");
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < data.size(); i++) {

			Record record = data.get(i);
			Map<String, Object> checkBoxData = new HashMap<String, Object>();
			String fieldname = record.getStr("fieldname");
			String unit = record.getStr("unitname").trim().equals("") ? record.getStr("unitname").trim()
					: "(" + record.getStr("unitname").trim() + ")";
			String fieldcode = record.getStr("fieldcode");

			checkBoxData.put("name", fieldname + unit);
			checkBoxData.put("code", fieldcode);

			dataList.add(checkBoxData);
		}

		json = JSON.toJSONString(dataList);
		 System.out.println(json);

		renderJson(json);

	}

}
